﻿using System;
using NSubstitute;
using Phoenix.Framework;
using Xunit;

namespace Phoenix.Tests
{
    public class ControllerTests : Controller
    {
        public ControllerTests()
        {
            this.Initialise("Controller.Action");
        }

        [Fact]
        public void page_method_builds_page_action_result()
        {
            // arrange
            var controller = this;

            // act
            var result = controller.Page();

            // assert
            Assert.NotNull(result);
        }

        [Fact]
        public void redirect_defaults_to_current_controller_when_not_specified()
        {
            // arrange
            var controller = this;

            // act
            var result = controller.RedirectTo("SomeAction");
            result.InvokeComplete();

            // assert
            Context.Host.ActionInvoker.Received().InvokeAction(Arg.Is<InvokeActionRequest>(a => a.Target.Controller == Context.ControllerAction.Controller));
            Context.Host.ActionInvoker.Received().InvokeAction(Arg.Is<InvokeActionRequest>(a => a.Target.Action == "SomeAction"));
        }

        [Fact]
        public void redirect_defaults_to_specified_controller_when_specified()
        {
            // arrange
            var controller = this;

            // act
            var result = controller.RedirectTo("SomeController.SomeAction");
            result.InvokeComplete();

            // assert
            Context.Host.ActionInvoker.Received().InvokeAction(Arg.Is<InvokeActionRequest>(a => a.Target.Controller == "SomeController"));
            Context.Host.ActionInvoker.Received().InvokeAction(Arg.Is<InvokeActionRequest>(a => a.Target.Action == "SomeAction"));
        }
    }
}